﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
public partial class Classified_popup_Cld_Upload_Image : System.Web.UI.Page
{

    string Link = string.Empty, strCode = string.Empty;
    HttpPostedFile myFileHeader = null;
    BusinessLogic busin = new BusinessLogic();
    const string constUser = "User";
    BO.Ad bo = new BO.Ad();
    BLL.AdMagnager bll = new BLL.AdMagnager();
    RegistrationData _objRegistrationData = new RegistrationData();
    int UserID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!object.Equals(Session[constUser], null))
        {
            _objRegistrationData = (RegistrationData)Session["User"];
            UserID = _objRegistrationData.UserID;

            if (!IsPostBack)
            {
                if (Request.QueryString["AdID"] != null && Request.QueryString["AdImageVideosID"] != null)
                {
                    int AdID = 0, AdImageVideosID = 0;
                    if (int.TryParse(Convert.ToString(Request.QueryString["AdImageVideosID"]), out AdImageVideosID) && int.TryParse(Convert.ToString(Request.QueryString["AdImageVideosID"]), out AdID))
                    {
                        //  BindData(AdID, AdImageVideosID);
                    }
                }
            }

        }
        else
        {
            lblMessage.Text = "You are logged out log in again to use this functionality";
        }
    }


    void BindData(int AdID, int AdImageVideosID)
    {
        DataSet dsImageVideos = new DataSet();
        bo.AdID = AdID;
        dsImageVideos = bll.Select_AdImageVideos(bo);
        if (!object.Equals(dsImageVideos, null))
        {
            if (dsImageVideos.Tables.Count > 0)
            {
                if (dsImageVideos.Tables[0].Rows.Count > 0)
                {
                    DataRow[] dr = dsImageVideos.Tables[0].Select("AdImageVideosID=" + AdImageVideosID.ToString());
                    if (dr.Length > 0)
                    {

                    }

                }
            }
        }

    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        if (!flImage.HasFile)
        {
            //_objRegistrationData.ProfileImage = Common.DefaultImage;
            lblMessage.Text = "Please select an image";

        }
        if (flImage.HasFile)
        {
            if (flImage.PostedFile != null)
            {
                string ext = string.Empty;
                ext = Path.GetExtension(flImage.FileName);
                ext = ext.ToLower();
                if (ext == ".jpg" || ext == ".jpeg" || ext == ".bmp" || ext == ".gif" || ext == ".png")
                {
                    myFileHeader = flImage.PostedFile;
                    int nFileLen = myFileHeader.ContentLength;
                    if (nFileLen > 0)
                    {
                        if (nFileLen < 4194304)
                        {
                            string ImageName = Path.GetFileName(flImage.PostedFile.FileName);
                            string folderPath = "AdImages/";
                            string ImagePath = folderPath + ImageName;

                            ImageName = busin.ValidateImageName(folderPath, ImageName);
                            ImagePath = folderPath + ImageName;
                            flImage.PostedFile.SaveAs(Server.MapPath("~/Classified/"+ImagePath));

                            if (Request.QueryString["AdImageVideosID"] != null)
                            {
                                int AdImageVideosID = 0;
                                if (int.TryParse(Convert.ToString(Request.QueryString["AdImageVideosID"]), out AdImageVideosID))
                                {
                                    bo.AdImageVideosID = AdImageVideosID;
                                    bo.Path = ImagePath;
                                    bll.Update_Image(bo);
                                }
                            }

                            string script = string.Format("javascript:parent.reload();parent.$.fancybox.close();");
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "alertScript", script, true);
                            //lblMessage.Text = "Image Updated Successfully";
                            //btnCancel.Attributes.Add("onclick", "javascript:window.parent.location = '/Reg-Update-Profile.aspx';");
                            //btnCancel.Attributes.Add("src", "images/close.jpg");
                        }
                    }
                }
            }
        }
    }
}